package com.ruoyi.consultation.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.consultation.mapper.LabConsultationInfoMapper;
import com.ruoyi.consultation.domain.LabConsultationInfo;
import com.ruoyi.consultation.service.ILabConsultationInfoService;
import com.ruoyi.labdata.domain.LabFile;
import com.ruoyi.labdata.service.ILabFileService;
import com.ruoyi.system.utils.FileTypeEnum;

/**
 * 信息咨询Service业务层处理
 * 
 * @author winner pan
 * @date 2022-03-28
 */
@Service
public class LabConsultationInfoServiceImpl implements ILabConsultationInfoService {
	@Autowired
	private LabConsultationInfoMapper labConsultationInfoMapper;
	@Autowired
    private ILabFileService labFileService;
	private static final String belongType = FileTypeEnum.Consultation.getValue();
	/**
	 * 查询信息咨询
	 * 
	 * @param consultationId
	 *            信息咨询ID
	 * @return 信息咨询
	 */
	@Override
	public LabConsultationInfo selectLabConsultationInfoById(Long consultationId) {
		List<LabFile> labFiles = labFileService.getFiles(consultationId, belongType);
		LabConsultationInfo info = labConsultationInfoMapper.selectLabConsultationInfoById(consultationId);
		info.setImages(labFiles);
		return info;
	}

	/**
	 * 查询信息咨询列表
	 * 
	 * @param labConsultationInfo
	 *            信息咨询
	 * @return 信息咨询
	 */
	@Override
	public List<LabConsultationInfo> selectLabConsultationInfoList(LabConsultationInfo labConsultationInfo) {
		
		return labConsultationInfoMapper.selectLabConsultationInfoList(labConsultationInfo);
	}

	/**
	 * 新增信息咨询
	 * 
	 * @param labConsultationInfo
	 *            信息咨询
	 * @return 结果
	 */
	@Override
	@Transactional
	public int insertLabConsultationInfo(LabConsultationInfo labConsultationInfo) {
		labConsultationInfo.setCreateTime(DateUtils.getNowDate());
		labConsultationInfo.setCreateBy(SecurityUtils.getLoginUser().getUsername());
		int ret = labConsultationInfoMapper.insertLabConsultationInfo(labConsultationInfo);
		long id = labConsultationInfo.getConsultationId();
		labFileService.insertFiles(labConsultationInfo.getImages(), id, belongType);
		return ret;
	}

	/**
	 * 修改信息咨询
	 * 
	 * @param labConsultationInfo
	 *            信息咨询
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateLabConsultationInfo(LabConsultationInfo labConsultationInfo) {
		List<LabFile> labFiles = labConsultationInfo.getImages();
		labFileService.updateFiles(labFiles, labConsultationInfo.getConsultationId(), belongType);
		return labConsultationInfoMapper.updateLabConsultationInfo(labConsultationInfo);
	}

	/**
	 * 批量删除信息咨询
	 * 
	 * @param consultationIds
	 *            需要删除的信息咨询ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteLabConsultationInfoByIds(Long[] consultationIds) {
		for (Long id : consultationIds) {
			labFileService.deleteLabFile(id, belongType);
		}
		return labConsultationInfoMapper.deleteLabConsultationInfoByIds(consultationIds);
	}

	/**
	 * 删除信息咨询信息
	 * 
	 * @param consultationId
	 *            信息咨询ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteLabConsultationInfoById(Long consultationId) {
		labFileService.deleteLabFile(consultationId, belongType);
		return labConsultationInfoMapper.deleteLabConsultationInfoById(consultationId);
	}
}
